<?php 

$_action = 'add';
$_type_id = tos_http_get('type_id');

$_thumbnail = $this->get_data('thumbnail');
if(empty($_thumbnail)) {
    $_thumbnail = 'img/blueprint.png';
}
if ($this->has_data('action')) {
    $_action = $this->get_data('action');
}
if ($this->has_data('type_id')) {
    $_type_id = $this->get_data('type_id');
}
$node_id = tos_http_get('node_id');
$node_keywords = $this->get_data('node_keywords');
$prev_node_mapping = $this->get_data('prev_node_mapping');

?>
<div id="ne_w">
<div id="pne"></div>
<div id="ne">
<h1 class="caption"><?php $this->write('page_caption')?></h1>
<div class="box">
<div class="thumbnail">
    <?php echo tos_img($_thumbnail, 0, 0, array('id' => 'thumbnail_img'));?>
</div>
<div>
    Caption:<br />
    <input type="text" class="text" id="node_caption" onchange="caption_changed();" value="<?php $this->write('caption');?>" />
</div>
<div>
    Url segment:<br />
    <input type="text" class="text" id="node_url_segment" onkeyup="url_sugment_customise();" value="<?php $this->write('url_segment');?>" />
</div>
<div>
    Description:<br />
    <textarea id="node_description" onchange="description_changed();"><?php $this->write('description')?></textarea>
</div>
<?php $this->display_box();?>


<form accept-charset="utf-8" name="node_form" method="post" action="<?php echo tos_url(array('m'=>'node', 'a'=>$_action, 'type_id'=>$_type_id, 'node_id' => $node_id));?>">
<div>
    <input type="submit" class="submit" name="submit" value="Save" /> 
    <input type="submit" class="submit" name="submit" value="Save & View" />
</div>
</form>
</div>
</div>
<div class="clear"></div>
</div>
<script type="text/javascript">
var knm,cdn;
function init () {
    if (undefined == knm) {
        knm = new z.knm(z.s('pne'), document.forms['node_form']);
        
        <?php if ('add' == tos_http_get('a')) : ?>
        knm.add_require('caption');
        <?php endif; ?>

<?php if ($prev_node_mapping): ?>
            <?php foreach ($prev_node_mapping as $nm):?>
                <?php 
                    $_keyword = isset($node_keywords[$nm['keyword_id']])? 
                        $node_keywords[$nm['keyword_id']]['keyword'] : '';
                ?>
                knm.add_kn('<?php echo $_keyword;?>', <?php echo json_encode($nm);?> );
<?php endforeach;?>
<?php endif;?>
        <?php if ($node_keywords): ?>
            <?php foreach ($node_keywords as $node_keyword): ?>
                knm.add_kn('<?php echo $node_keyword['keyword']; ?>');
            <?php endforeach;?>
        <?php endif;?>

        <?php 
        if ($this->has_data('parent_node')) { 
            $parent_node = $this->get_data('parent_node');
        ?>
        knm.add_kn(
            '<?php echo tos_http_get('keyword');?>',
            {
                'id':             "<?php echo $parent_node['id']; ?>",
                'caption':        "<?php echo tos_html_strip($parent_node['caption']); ?>",
                'keywords':       "<?php echo tos_html_strip($parent_node['keywords']); ?>",
                'description':"<?php echo tos_html_strip($parent_node['description']); ?>"
            });
        <?php } ?>
        knm.add_kn();
        cdn = new z.cdn();
        cdn.set_pos(0, 100);
        var thumbnail_img = z.s('thumbnail_img');
        thumbnail_img.onclick = function () {
            cdn.show(null, function () {
                //alert(this.img_cdn_path);
                thumbnail_img.src = z.cdn_url(this.img_cdn_path);
                knm.set('node_thumbnail', this.img_cdn_path);
                this.hide();
            });
        }
    }
}
function caption_changed() {
    //var form = document.forms['node_form'];
    var url_segment = z.s('node_url_segment');
    //var url_segment_str = z.trim(url_segment.value);
    var caption_str = z.trim(z.s('node_caption').value);
    knm.set('caption', caption_str);
    if ( ! url_segment.customised) {
        var url_segment_str = caption_str.replace(/[^a-z0-9]+/ig, '-');
        url_segment.value = z.trim(url_segment_str.toLowerCase(), '-');
        knm.set('url_segment', url_segment.value);
    }
}
function url_sugment_customise() {
    var url_segment = z.s('node_url_segment');
    var url_segment_value = z.trim(url_segment.value);
    if (url_segment_value !== '') {
        url_segment.customised = true;
        url_segment.value = url_segment_value.replace(/[^a-z0-9]+/ig, '-').toLowerCase();
        knm.set('url_segment', url_segment.value);
    }
    else {
        url_segment.customised = false;
        caption_changed();
    }
}
function description_changed() {
    var description = z.s('node_description');
    knm.set('description', description.value);
}

init();
</script>